package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.BookDao;
import com.oocl.pojo.Book;
import com.oocl.util.DBUtil;

public class BookDaoImpl  implements BookDao{

	public List<Book> findAll() {
		String sql = "select * from book";
		List<Book> list = new ArrayList<Book>();
		Connection con = null;
		PreparedStatement stat =null;
		ResultSet rs = null;
        try {
        	con = DBUtil.createConnection();
			stat = con.prepareStatement(sql);
			rs = stat.executeQuery();
			while(rs.next()){
				Book b = new Book();
				b.setId(rs.getString("id"));
				b.setName(rs.getString("name"));
				b.setNum(rs.getInt("num"));
				//System.out.println(rs.getString("filename"));
				b.setFileName(rs.getString("filename"));
				list.add(b);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(con, stat, rs);
			
		}
		return list;
	}

	public Book loadBook(String bId) {
		String sql = "select * from book where id=?";
		Book b = new Book();
		Connection con = null;
		PreparedStatement stat =null;
		ResultSet rs = null;
        try {
        	con = DBUtil.createConnection();
			stat = con.prepareStatement(sql);
			stat.setString(1, bId);
			rs = stat.executeQuery();
			if(rs.next()){
				b.setId(rs.getString("id"));
				b.setName(rs.getString("name"));
				b.setNum(rs.getInt("num"));
				System.out.println(rs.getString("filename"));
				b.setFileName(rs.getString("filename"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(con, stat, rs);
			
		}
		return b;
	}

}
